package com.atguigu.gulimall.order.config.alipay;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimall.order.vo.alipay.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")// 这个配置的意思是我们本类所有属性的配置可以跟配置文件的alipay进行匹配
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2016101900719881";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCACE+Fb/7qthzSgn9BhBSiko1IX1rNoZXMV+le9ujKYOmQaDq/PBiX4kAcMAhW0oDZ27aCQzAZIvr4jeX+Ov2mQGTbgJBtmvwwpU8ovg40dLu81Cn8Cq7Wp8+N79PI7n4CrNHWVgh0MhQfSLEW0zvVZ0TdSAhQ5dyOSnBZz4QUshd1JfT/miL67/WL9g2Y8ttZuRcRpNcWd+YM8d0lc9ACL20qAPjJstI/EL9D+mIXWkNR+wwBSLN8cueAvIJA7mNPYx/Z7cDdQw6EpEFbvIcl2NUqR3TJUH2wnm6HSviFpXnbY8HWU+AfNllrqQ1X9ywaOWrw5LzaPsT5nZE8gBVlAgMBAAECggEAeStkVx0PXILN9EycsxyViua3326fOU1bv/DTZJS47A+b/JcoQiRWAiiWFBdBTSob2BMgP1fnBWMdsm5Y5KpftB/tU+/wwEqVslRA/oYgW3S+2I7SzT4vCef3fpUjEl7j0xRL41W42n8qnBDnyHpRfj4l5mZtuGJSwjeg7N6AWjR7W5D9s5bQxk4bC/Rlx5cx78ZgXYZGHoZRXTx/pXpjFd4IRJ0aOfwRwXCUmLWtJRhSOFtPQD4qpley+x64SzYVWCPIFwp8lVdOcQoZGIwm25YjKfszyF3UrFqK9Jwd5+4T/L8szGJZFpBf6CMLA/+mtukPrW0tWkWSirbSsY3MAQKBgQC2+6B7ofp6Eoi4j56FOspLhtocuaOcxhoFu2Ic+0ATgbP+XKTsoiQi4MOHzpMWh9C2VJAkYzk9oQ35+fbQ3YqEVltK4dn6wrBUxXFuP+xqLdnucTT3Thw+yIkkQ8kLnYf8YaV33YwjV84mM/yKCFVH3n20cGPJCT6BD1gmiF4whQKBgQCzH0tSUVrURjdauk4l4yPuyr82bFFAgHUwPnwu1oW4q6rB5rq4FzhB87z2cBxqdJqXdExDqTtMXa3trg7y9PrM/AfdLeNrGlNx1DrOeZmAW8KL/CQ7GO50bBC0B1EcvDdDHlUVID31I7DACx5RbC7oEcyoYDlBco2ig8XoAtDXYQKBgDRMSLrtObSoFXGCgtEbU3Qwr6gkGrIVIJzgyJL18INiv7oEapLDaMabLOdk0D9RRALIuoXb4iFOJbt7iOJ7Xzqhn+gQEwgLmvAvmlLjbEOQpUf0DSpdpc7/68p0oUJznMkyc2OF66ayUtYyZ4xgA1fOLGlk8fAkdzvV0eD5esotAoGABbqKfid78C4OtygeN7f7/jcdxm/QaW+yT8iv0A+sVyumWm02q4ySkAlrkbYkMoNoLk/Z4JAZBWg5l9BpSfvryYKF/dzp2RMOeCWBe/kHn8NPhYqa3gj+F1sdbaSOQ40Bd3QQ5IwTbzOlA39XiOMc3O80zG3HZwBy5y+vtKviYgECgYBhztuDU99JJdNtD5LLNX0wjM4Vc2PGh+MVlxirDvuo1vCqTV0B4ah6cPB6odR8GCSNC7ngrVqtfSLqxLNrq02LjCS1ioRxvf2XP4g1mNa02bm5HAFhja30jonzI7R6JdkflCaDNAC1A5nVe0GcZVtNpJ8+p/fHrESYOb9azJ8W3w==";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAhiwoozDJlrJyB9WZMlVcwUPNDJSWCKXa8bAWcWDG0FK7ACsngbn8fzzEzWJO3vgi46IW/AiYm7KPGLYWe7WhS1R7pMoHkb5inAubht5jyObU2V6hHyuc2tRVMYtwkfL/lODqfoGeeKtpjdoZeeR+i29io63meXqjmHv3ibmxSTeZ28PdRKP0TBdWNwy5QopymQfG1LBW6BSYmFsLrGf9+BV2EvAV8O0YZffm6kpKHE6KGVXKtV7hlfaaOiYoqSn4D8SpxrYUBSEQziWLWj4FiCV5wKsazsMH0AjBjmkRfiLc10PdtgReHy8RZb6qCJGonGO0jbiqULG36Y2EYKvbDwIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url = "http://1doc1q8c1v.52http.tech/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url = "http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    //收单，超时时间
    private String timeout = "30m";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+timeout+"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
